/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JChildDAOEditorPanel.java
 *
 * Created on 2010-10-6, 10:44:06
 */
package cn.ekuma.data.ui.swing;

import e.odbo.data.bean.I_BaseBean;
import e.odbo.data.dao.ChildEditorRecord;
import e.odbo.data.dao.I_DataLogic;
import e.odbo.data.dao.I_ParentNode;
import e.odbo.data.dao.ParentNode;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.JMessageDialog;
import com.openbravo.data.gui.MessageInf;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class JChildDAOEditorPanel<K> extends AbstractJXTablePanel implements ParentNodeChangeListener<K> {

    protected AbstractDAOJEditor editor;
    protected JEditorWarpDialog editorDialog;
    protected I_DataLogic dlSales;
    protected AbstractDTOTableModel tableModel;

    public JChildDAOEditorPanel(I_DataLogic dlSales, AbstractDTOTableModel<K> tableModel, AbstractDAOJEditor editor) {
        super();
        this.editor = editor;
        this.dlSales = dlSales;
        this.tableModel = tableModel;
        editorDialog = JEditorWarpDialog.getInstance(this).init(dlSales, editor);
        getJXTable().setModel(tableModel);
    }

    public void activate() throws BasicException{
        editor.activate();
    }

    @Override
    public boolean removeSelectedObjects(int[] rows) {
        List objs = new ArrayList();
        for (int i : rows) {
            objs.add(tableModel.getObj(i));
        }
        try {
            for (Object obj : objs) {
            	if(dlSales!=null)
                   dlSales.delete((I_BaseBean) obj);
                tableModel.remove(obj);
            }
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
            return false;
        }
        return true;
    }

    @Override
    public void addObject() {
    	try {
			editorDialog.activate();
		} catch (BasicException e) {
			 new MessageInf(e).show(this);
			 return;
		}
        editorDialog.writeValueInsert(null);
        if (editorDialog.getRetObj() != null) {
            tableModel.add(editorDialog.getRetObj());
        }
    }

    public String getTabName() {
    	if(editor instanceof ChildEditorRecord)
           return ((ChildEditorRecord)editor).getTabName();
    	return "no define";
    }

    public void parentNodeValueChange(int state, I_ParentNode<K> parent) {
    	parent=new ParentNode<K>(parent);
        if(editor instanceof ParentNodeChangeListener)
        	((ParentNodeChangeListener)editor).parentNodeValueChange(state, parent);
        if (state == AbstractChildDAOJEditor.STATE_Edit) {
            updateUI(true);
            try {
                tableModel.resert(dlSales.listByParent(editor.getEditClass(), parent));
            } catch (BasicException ex) {
                JMessageDialog.showMessage(JChildDAOEditorPanel.this, new MessageInf(ex));
            }
        } else {
            updateUI(false);
            tableModel.resert(null);
        }
    }

    @Override
    public void editObjectAt(int row) {
    	try {
			editorDialog.activate();
		} catch (BasicException e) {	
			 new MessageInf(e).show(this);
			 return;
		}
       editorDialog.writeValueEdit(tableModel.getObj(row));
       if (editorDialog.getRetObj() != null) {
            tableModel.fireTableRowsUpdated(row, row);
       }
    }
}
